// 侧边栏功能
const sidebar = document.getElementById("sidebar");
const sidebarToggle = document.getElementById("sidebarToggle");
const closeSidebar = document.getElementById("closeSidebar");
const loginBtnDisplay = document.getElementById("login");
const isLoggedIn = localStorage.getItem("isLoggedIn");
if (isLoggedIn === "true") {
    if (loginBtnDisplay) {
        loginBtnDisplay.style.display = "none";
    }
}

if (sidebarToggle) {
    sidebarToggle.addEventListener("click", () => {
        sidebar.classList.add("active");
    });
}

if (closeSidebar) {
    closeSidebar.addEventListener("click", () => {
        sidebar.classList.remove("active");
    });
}

// 表单验证函数
function validateEmail(email) {
    const re =
        /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
    return re.test(email);
}

function validatePassword(password) {
    return password.length >= 8;
}

// 其他公共功能...
function logout() {
    localStorage.removeItem("isLoggedIn");
    localStorage.removeItem("currentUser");
    window.location.href = "login.html";
}
function updateUserUI() {
    const isLoggedIn = localStorage.getItem("isLoggedIn") === "true";
    const userInfo = document.getElementById("userInfo");
    const loginBtn = document.getElementById("loginBtn");
    console.log("isLoggedIn:", isLoggedIn);

    if (isLoggedIn) {
        const currentUser = JSON.parse(localStorage.getItem("currentUser"));
        if (userInfo) {
            userInfo.style.display = "flex";
            userInfo.querySelector(".username").textContent =
                currentUser.username;
        }
        if (loginBtn) loginBtn.style.display = "none";
    } else {
        if (userInfo) userInfo.style.display = "none";
        if (loginBtn) loginBtn.style.display = "block";
    }
}

// 页面加载时检查登录状态
document.addEventListener("DOMContentLoaded", function () {
    updateUserUI();
});
